package com.mjduan.myBank.user.service.serviceImpl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mjduan.myBank.user.dao.IUserDao;
import com.mjduan.myBank.user.service.IUserService;
import com.mjduan.myBank.util.entity.User;
import com.mjduan.myBank.util.property.MyProperty;
import com.mjduan.myBank.util.util.StringUtil;

@Service("UserServiceImpl")
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements IUserService {

    @Autowired
    @Qualifier("UserDaoImpl")
    private IUserDao userDao;

    @Override
    public Map<String, Object> login(User user) {
        Map<String, Object> map = new HashMap<String, Object>();

        User tempUser = userDao.findUserByAccountAndPassword(user);
        if (tempUser == null) {
            map.put(MyProperty.STATUS, MyProperty.FAIL);
            map.put(MyProperty.FAIL_MESSAGE, "账号或密码错误!");
        } else if (tempUser.getStatus() == MyProperty.MAGIC_NUMBER_ONE) {
            //邮箱已经被激活了
            map.put(MyProperty.STATUS, MyProperty.SUCCESS);
            map.put("user", tempUser);
        } else {
            //邮箱还没有被激活
            map.put(MyProperty.STATUS, MyProperty.FAIL);
            map.put(MyProperty.FAIL_MESSAGE, "你的邮箱还没有激活，请先激活邮箱!");
        }

        return map;
    }

    @Override
    public Map<String, Object> registeUser(User user) {
        Map<String, Object> map = new HashMap<String, Object>();

        if (userDao.isNameUsed(user.getName())) {
            map.put(MyProperty.STATUS, MyProperty.FAIL);
            map.put(MyProperty.FAIL_MESSAGE, "注册失败，该用户名已经注册过了，请换一个用户重新注册!");
            return map;
        }
        if (userDao.isEmailUsed(user.getEmail())) {
            map.put(MyProperty.STATUS, MyProperty.FAIL);
            map.put(MyProperty.FAIL_MESSAGE, "注册失败，该邮箱已经注册过了，请换一个邮箱重新注册!");
            return map;
        }
        /*注意单一职责，userDao.addUser方法的职责只是添加user*/
        if (userDao.addUser(user)) {
            User tempUser = userDao.findUserByAccountAndPassword(user);
            map.put(MyProperty.STATUS, MyProperty.SUCCESS);
            map.put(MyProperty.FAIL_MESSAGE, "注册成功!");
            map.put("user", tempUser);

            return map;
        } else {
            map.put(MyProperty.STATUS, MyProperty.FAIL);
            map.put(MyProperty.FAIL_MESSAGE, "服务器内部错误，已经联系管理员了，请稍候重新注册!");

            return map;
        }
    }

    @Override
    public boolean isEmailUsed(String email) {
        // TODO Auto-generated method stub
        if (StringUtil.isEmpty(email)) {
            return false;
        }

        return this.userDao.isEmailUsed(email);
    }

    @Override
    public boolean updateUserStatusAfterEmailConfirm(int id, String password) {
        // TODO Auto-generated method stub
        if (StringUtil.isEmpty(password)) {
            return false;
        }

        return this.userDao.updateUserStatus(id, password, MyProperty.MAGIC_NUMBER_ONE);
    }

}
